﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WxImport
{
    public static class ThreadHelper
    {
        public delegate void TaskDelegate();

        private delegate void InvokeMethodDelegate(System.Windows.Forms.Control control, TaskDelegate handler);
        /// <summary>
        /// 多线程操作时安全访问控件
        /// </summary>
        /// <param name="form">窗体</param>
        /// <param name="action">操作</param>
        public static void AccessControl(Form form, Action action)
        {
            if (form.InvokeRequired)
            {
                form.Invoke((MethodInvoker)action.Invoke);
            }
            else
            {
                action.Invoke();
            }
        }
        public static void AccessControl(System.Windows.Forms.Control ctrl, Action action)
        {
            if (ctrl.InvokeRequired)
            {
                ctrl.Invoke((MethodInvoker)action.Invoke);
            }
            else
            {
                action.Invoke();
            }
        }
        /// <summary>
        /// 线程安全访问控件,扩展方法.net3.5用Lambda简化跨线程访问窗体控件,避免重复的delegate,Invoke
        /// this.statusStrip1.SafeInvoke(() =>
        /// {
        ///     tsStatus.Text = "开始任务....";
        /// });
        /// this.rtxtChat.SafeInvoke(() =>
        /// {
        /// rtxtChat.AppendText("测试中");
        /// });
        /// </summary>
        public static void SafeInvoke(this System.Windows.Forms.Control control, TaskDelegate handler)
        {
            try
            {
                if (!control.IsHandleCreated)
                    return;

                if (control.IsDisposed)
                    return;
                if (control.InvokeRequired)
                {
                    while (!control.IsHandleCreated)
                    {
                        if (control.Disposing || control.IsDisposed)
                            return;
                    }
                    IAsyncResult result = control.BeginInvoke(new InvokeMethodDelegate(SafeInvoke),
                        new object[] { control, handler });
                    control.EndInvoke(result); //获取委托执行结果的返回值
                    return;
                }

                IAsyncResult result2 = control.BeginInvoke(handler);
                control.EndInvoke(result2);
            }
            catch (System.ObjectDisposedException exDispose)
            {

            }
            catch (Exception ex)
            {

            }

        }
        public static void EnableButton(Form form, Button btn, string szButtonText = "开始生成")
        {
            AccessControl(form, () =>
            {
                btn.Enabled = true;
                btn.Text = szButtonText;
            });
        }


        public static void DisableButton(Form form, Button btn, string szButtonText = "正在处理，请稍等..")
        {
            AccessControl(form, () =>
            {
                btn.Enabled = false;
                btn.Text = szButtonText;
            });
        }

    }
}
